<html>
  <head>      
    <script language="javascript" src="js/processing.js"></script>
    <script language="javascript" src="js/init.js"></script>
    <script language="javascript" src="../dsp.js"></script>
  </head>
  <body>
    <style>
      body { background: black; margin:0; padding:0;}
    </style>
    
    <script>
      var frameBufferSize = 4096;
      var bufferSize = frameBufferSize/2;
      
      var signal = new Float32Array(bufferSize);
      var peak = new Float32Array(bufferSize);
      
      var fft = new RFFT(bufferSize, 44100);

      function loadedMetaData(event) {
        var audio = document.getElementById('input');
        audio.mozFrameBufferLength = frameBufferSize;
        audio.addEventListener("MozAudioAvailable", audioAvailable, false);
      }

      function audioAvailable(event) {
        // deinterleave and mix down to mono
        signal = DSP.getChannel(DSP.MIX, event.frameBuffer);

        // perform forward transform
        fft.forward(signal);
        
        // calculate peak values
        for ( var i = 0; i < bufferSize; i++ ) {
          fft.spectrum[i] *= -1 * Math.log((fft.bufferSize/2 - i) * (0.5/fft.bufferSize/2)) * fft.bufferSize; // equalize, attenuates low freqs and boosts highs
          
          if ( peak[i] < fft.spectrum[i] ) {
            peak[i] = fft.spectrum[i];
          } else {
            peak[i] *= 0.99; // peak slowly falls until a new peak is found
          }
        }
      }
    </script>
    
    <script target="#fft" type="application/processing">
      void setup() {
        size(1400, 600);
        colorMode(HSB, 360, 100, 100);
        strokeWeight(3);
        frameRate(60);
        //strokeCap(SQUARE);
      }
      
      void draw() {
        background(0);
        //fill(0, 60);
        //rect(0, 0, width, height);
        for ( int i = 0; i < fft.spectrum.length/2; i += 3 ) {          
          if (3 * i > width) { break; }
          var magnitude = fft.spectrum[i];
          
          // draw magnitudes
          stroke((i) % 360, 60, constrain(magnitude * 6, 20, 100));
          line(3*i, height, 3*i, height - magnitude * 16);
          
          // draw peak indicators
          stroke((i) % 360, 60, constrain(magnitude * 100, 50, 100));
          line(3*i, height - peak[i] * 16 - 1, 3*i, height - peak[i] * 16);
        }
        /*
        if (frameCount % 100 == 0 ) {
          println(FRAME_RATE);
        }
        */
      } 
    </script>
    
    <audio id="input" src="audio/corban-peddle.ogg" controls="true" onloadedmetadata="loadedMetaData(event);"></audio><br />
    
    <div><canvas id="fft"></canvas></div>
  </body>
</html>
